#include <stdio.h>
#include <string.h>

#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//hash.h"
#include "..//include//vfs_errorcodes.h"
#include "..//include//fileOperations.h"

struct Block b;
struct header headervar;
struct FD file_desc;	


//FILE *fp_src; 
//FILE *fpr_dest;

int removefileop(char file_path[], char filename_g[]) {


	int blkno;	
	char completePath[100]="";
	int used_des;
	int j;
	int flag;
	flag=0; // To check if the file exists or not
	
	FILE *fp_src; 
	FILE *fpr_dest;
	
	
	
	if ((strcmp(file_path,"") == 0)){
		printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_00);
		return 0;
	}
	
	fpr_dest = fopen(filename_g, "rb+");
	
	if (fpr_dest == NULL){ 
		//printf("\n\n\t Unable to open file.");
		return 0;
	}
	
	if(strcmp(file_path,"/")==0){
		printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_01);
		return 0;	
	}
		
	else{
		
		rewind (fpr_dest);
		fread(&headervar, sizeof(struct header), 1, fpr_dest);		
		used_des = headervar.used_desc;
		
		rewind(fpr_dest);
		fseek(fpr_dest, sizeof(struct header), SEEK_SET);
		
		fread(&freelist,sizeof(freelist), 1, fpr_dest);		
		
		
			 	
		//Update the FD
		rewind (fpr_dest);		
		fseek (fpr_dest, LABEL_SIZE*sizeof(char) + 2*(sizeof(int)) + sizeof(struct FD), SEEK_SET);
		
		
			 	for(j=2; j<=TOTAL_DESC; j++){
			 		fread (&file_desc, sizeof(struct FD), 1, fpr_dest);
			 		//printf("Inside removefile: %s, %s\n",file_desc.path, file_path);
			 		if(strcmp(file_desc.path,file_path)==0){
			 			
			 			//printf("Found!!\n");
			 			flag = 1;
			 			
			 			blkno = file_desc.blockno;			 			
			 			
  						strcpy(file_desc.path,"\0");
  						strcpy(file_desc.name,"\0");
  						strcpy(file_desc.type,"\0");
  						file_desc.blockno = 0;
  				
  					fseek (fpr_dest, LABEL_SIZE*sizeof(char) + 2*(sizeof(int))+ sizeof(struct FD)*(j-1), SEEK_SET); 
		  				fwrite(&file_desc, sizeof(struct FD), 1, fpr_dest);
  						rewind (fpr_dest);
  				
  						freelist[blkno-1] = 0; //VERY IMPORTANT! This indicates that the block is now free.	
  						
  						rewind(fpr_dest);
						fseek(fpr_dest, sizeof(struct header), SEEK_SET);				
						fwrite(&freelist, sizeof(freelist),1, fpr_dest);
						
						num_used_blocks = num_used_blocks - 1;
  							
			 			break;
			 		}
			 		
			 		
			 				 		
			 	}
			 	
			 	if (flag == 0){
			 		printf("removefile_FAILURE %s\n",ERR_VFS_REMOVEFILE_01);  // Cannot find file
					return 0;
			 		
			 	}
			 				 
			 				 
		// Deleting the data from the blocks			 
			 				 	
		fseek(fpr_dest, sizeof(struct header)+(sizeof(freelist))+sizeof(struct Block)*(blkno-1),SEEK_SET);
		fread(&b, sizeof(struct Block), 1, fpr_dest);
		strcpy(b.blk,"\0");	
		fseek(fpr_dest, sizeof(struct header)+(sizeof(freelist))+sizeof(struct Block)*(blkno-1),SEEK_SET);	
		fwrite(&b, sizeof(struct Block), 1, fpr_dest);
			 	
			 				
		// Decrement the no. of used descriptors
			
		rewind (fpr_dest);
		fread(&headervar, sizeof(struct header), 1, fpr_dest);		
		used_des = used_des - 1; 
		headervar.used_desc = used_des; //IMPORTANT! Decrement the no. of used descriptors.	
		rewind (fpr_dest);
		fwrite(&headervar, sizeof(struct header), 1, fpr_dest);
		
		
	}
	
	// Delete from the nary tree
	
	
	root = del_nary(root,file_path);	
	deletefile(file_path);
	
	
	fclose(fpr_dest);
	return 1;


}
